package com.gadgets.framework.common.util;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-10-10
 *
 */
public class StringUtils {

    /**
     * 分割字符串
     * @param targets   目标字符串
     * @return          分割结果
     */
    public static String[] splitStr(String targets){
        if(!org.springframework.util.StringUtils.isEmpty(targets)) {
            return targets.split(",");
        }
        return new String[0];
    }

    /**
     * 分割字符串为Long型
     * @param targets   目标字符串
     * @return          分割结果
     */
    public static Long[] splitStrToLong(String targets){
        if(!org.springframework.util.StringUtils.isEmpty(targets)) {
            String[] retStr = targets.split(",");
            Long[] ret = new Long[retStr.length];
            for (int i = 0; i < retStr.length; i++) {
                ret[i] = Long.parseLong(retStr[i]);
            }
            return ret;
        }
        return new Long[0];
    }

    /**
     * 分割字符串为Int型
     * @param targets   目标字符串
     * @return          分割结果
     */
    public static Integer[] splitStrToInt(String targets){
        if(!org.springframework.util.StringUtils.isEmpty(targets)) {
            String[] retStr = targets.split(",");
            Integer[] ret = new Integer[retStr.length];
            for (int i = 0; i < retStr.length; i++) {
                ret[i] = Integer.parseInt(retStr[i]);
            }
            return ret;
        }
        return new Integer[0];
    }

    /**
     * 字符串判空
     * @param target    目标字符串
     * @return          是否为空
     */
    public static boolean isBlank(String target){
        return null==target || org.springframework.util.StringUtils.isEmpty(target) || target.length()==0;
    }

    /**
     * 格式化异常栈信息
     * @param throwable 异常信息
     * @return          异常栈信息
     */
    public static String formatExceptionStack(Throwable throwable){
        StackTraceElement[] elements = throwable.getStackTrace();
        StringBuilder builder = new StringBuilder();
        // 字符串形式拼接错误栈信息
        builder.append("\n").append(throwable.getClass().getName()).append(": ").append(throwable.getMessage()).append("\n");
        if(null!=elements){
            for (StackTraceElement element : elements) {
                builder.append("\tat\t").append(element.toString()).append("\n");
            }
        }
        return builder.toString();
    }

}
